package org.emergency.framework.model;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.emergency.framework.constant.ModelConstant;

/**
 * 
 * @author Administrator
 * 
 */
@Entity
@Table(name = "TB_PERMISSION",catalog = ModelConstant.DB_CATALOG)
public class TbPermission implements java.io.Serializable {
	// Fields

	@Id
	@Column(name = "P_ID", unique = true, nullable = false, length = ModelConstant.STRING_LENGHT_id)
	private String PId;

	@Column(name = "P_NAME", nullable = false, length = 30)
	private String PName;

	@Column(name = "description", length = ModelConstant.STRING_LENGHT_4)
	private String description;

	@Column(name = "isEnabled")
	private boolean isEnabled;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "TB__P_ID")
	private TbPermission parentPermission; // 当前role的父亲节点

	@Transient
	private List<TbPermission> childPermission = new LinkedList<TbPermission>();

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "tbPermission")
	private Set<TbRolePermission> tbRolePermissions = new HashSet<TbRolePermission>(
			0);	//和角色权限的中间表

	/** default constructor */
	public TbPermission() {
	}

	/** minimal constructor */
	public TbPermission(String PId, String PName, String PResource,
			String PAction) {
		this.PId = PId;
		this.PName = PName;
	}

	/** full constructor */
	public TbPermission(String PId, String PName, String PResource,
			String PAction, String a, String b, String c,
			Set<TbRolePermission> tbRolePermissions) {
		this.PId = PId;
		this.PName = PName;
		this.tbRolePermissions = tbRolePermissions;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((PId == null) ? 0 : PId.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TbPermission other = (TbPermission) obj;
		if (PId == null) {
			if (other.PId != null)
				return false;
		} else if (!PId.equals(other.PId))
			return false;
		return true;
	}

	// Property accessors

	public TbPermission getParentPermission() {
		return parentPermission;
	}

	public void setParentPermission(TbPermission parentPermission) {
		this.parentPermission = parentPermission;
	}

	public String getPId() {
		return this.PId;
	}

	public void setPId(String PId) {
		this.PId = PId;
	}

	public String getPName() {
		return this.PName;
	}

	public void setPName(String PName) {
		this.PName = PName;
	}

	public Set<TbRolePermission> getTbRolePermissions() {
		return this.tbRolePermissions;
	}

	public void setTbRolePermissions(Set<TbRolePermission> tbRolePermissions) {
		this.tbRolePermissions = tbRolePermissions;
	}

	public boolean isEnabled() {
		return isEnabled;
	}

	public void setEnabled(boolean isEnabled) {
		this.isEnabled = isEnabled;
	}

	public List<TbPermission> getChildPermission() {
		return childPermission;
	}

	public void setChildPermission(List<TbPermission> childPermission) {
		this.childPermission = childPermission;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	

}